package com.atguigu.tingshu.order.strategy.impl;
import java.math.BigDecimal;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.hander.OrderTradeNoHandler;
import com.atguigu.tingshu.order.helper.SignHelper;
import com.atguigu.tingshu.user.client.VipServiceConfigFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * VipOrderTradeStrategy vip购买策略类
 *
 * @author Petrichor
 * @version 2024/01/05 20:45
 **/
@Service
@Slf4j
public class VipOrderTradeStrategy extends OrderTradeNoHandler implements OrderTradeStrategy {

    private final VipServiceConfigFeignClient vipServiceConfigFeignClient;
    private final RedisTemplate<String,String> redisTemplate;
    @Autowired
    public VipOrderTradeStrategy(VipServiceConfigFeignClient vipServiceConfigFeignClient, RedisTemplate<String, String> redisTemplate) {
        this.vipServiceConfigFeignClient = vipServiceConfigFeignClient;
        this.redisTemplate = redisTemplate;
    }

    /**
     * @description: 定义获取订单确认页面所需要的接口方法
     * @author Petrichor
     * @date 2024/1/5 20:43
     * @param tradeVo 订单确认对象
     * @return OrderInfoVo 订单对象
     **/
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        // 付款项目类型Id
        Long itemId = tradeVo.getItemId();
        // 调用远程接口  通过id 获取vip服务配置信息
        Result<VipServiceConfig> result = vipServiceConfigFeignClient.findById(itemId);
        VipServiceConfig vipServiceConfig = result.getData();
        OrderInfoVo orderInfoVo = new OrderInfoVo();

        // <3> 付款项目类型
        orderInfoVo.setItemType(tradeVo.getItemType());
        // <4> 订单原始金额
        orderInfoVo.setOriginalAmount(vipServiceConfig.getPrice());
        // <5> 减免金额
        orderInfoVo.setDerateAmount(vipServiceConfig.getPrice().subtract(vipServiceConfig.getDiscountPrice()));
        // <6> 订单总金额
        orderInfoVo.setOrderAmount(vipServiceConfig.getDiscountPrice());
        // <7> 订单明细列表
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(itemId);
        orderDetailVo.setItemName(vipServiceConfig.getName());
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVo.setItemPrice(vipServiceConfig.getPrice());
        List<OrderDetailVo> orderDetailVos = Arrays.asList(orderDetailVo);
        orderInfoVo.setOrderDetailVoList(orderDetailVos);

        // <8> 订单减免明细列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>() ;
        BigDecimal derateAmount = orderInfoVo.getDerateAmount();
        // 如果减免金额存在 才进入 设置
        if(derateAmount.intValue() > 0) {
            OrderDerateVo orderDerateVo = new OrderDerateVo() ;
            // 设置订单减免类型 此处是vip服务折
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT);
            // 设置减免金额
            orderDerateVo.setDerateAmount(derateAmount);
            // 备注
            orderDerateVo.setRemarks("vip折扣");
            orderDerateVoList.add(orderDerateVo) ;
        }
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);



        // <1>交易号
        // <2>支付方式：1101-微信 1102-支付宝 1103-账户余额
        // <9>时间戳
        // <10>签名
        this.orderTradeNoHandle(orderInfoVo , redisTemplate);
        return orderInfoVo;
    }
    /**
     * @description: 定义获取对应的付款项目
     * @author Petrichor
     * @date 2024/1/5 20:44
     * @return 获取对应的付款项目
     **/
    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_VIP;
    }
}


